#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Node {
	string name;
	int score;
}node[10005];
bool cmp(Node a, Node b)
{
	if (a.score == b.score)
	{
		return a.name < b.name;
	}
	return a.score > b.score;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		cin >> node[i].name >> node[i].score;
	}
	sort(node, node + n, cmp);
	for (int i = 0;i < n;i++)
	{
		cout << node[i].name <<' '<< node[i].score << endl;
	}
	return 0;
}